package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Pdfl2;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class Pdfl2Form extends BasePage implements Serializable {
    private GenericManager<Pdfl2, Long> pdfl2Manager;
    private Pdfl2 pdfl2 = new Pdfl2();
    private Long id;

    public void setPdfl2Manager(GenericManager<Pdfl2, Long> pdfl2Manager) {
        this.pdfl2Manager = pdfl2Manager;
    }

    public Pdfl2 getPdfl2() {
        return pdfl2;
    }

    public void setPdfl2(Pdfl2 pdfl2) {
        this.pdfl2 = pdfl2;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        pdfl2Manager.remove(pdfl2.getId());
        addMessage("pdfl2.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            pdfl2 = pdfl2Manager.get(id);
        } else {
            pdfl2 = new Pdfl2();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (pdfl2.getId() == null || pdfl2.getId() == 0);
        
        pdfl2.setUsername(getRequest().getRemoteUser());
        pdfl2.setOptdate(new Date());
        pdfl2.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        pdfl2Manager.save(pdfl2);

        String key = (isNew) ? "pdfl2.added" : "pdfl2.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	  this.id=this.pdfl2.getId();
              return edit();
        }
    }
} 